package com.feiyangedu;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//Pattern 和 Matcher 组提取
public class Tel {
    static Pattern p  =Pattern.compile("^(0\\d{2,4})\\-([1,9]\\d{5,7})$");

    public  static Tel parse(String s){
        Matcher m = p.matcher(s);
        if(m.matches()){
            String  s1 = m.group(1);
            String s2 = m.group(2);
            return  new Tel(s1, s2);
        }
        return  null;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public String getPhone() {
        return phone;
    }

    private final  String areaCode ;
    private  final String phone;

    public  Tel(String areaCode, String phone){
        this.areaCode = areaCode;
        this.phone =phone;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.areaCode, this.phone);
    }

    @Override
    public boolean equals(Object obj) {
       if (this == obj){
           return  true;
       }
       if(obj instanceof  Tel){
           return Objects.equals(this.areaCode, ((Tel) obj).areaCode) &&
                   Objects.equals(this.phone, ((Tel) obj).phone);
       }
       return  false;
    }

    @Override
    public String toString() {
        return this.areaCode + "-" + this.phone;
    }
}
